package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.ProLearningRoute;
import com.wolves.career.entity.ProMajor;
import com.wolves.career.service.ProLearningRouteService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学习路线 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-31
 */
@RestController
@RequestMapping("/career/route")
@CrossOrigin
public class ProLearningRouteController {

    @Autowired
    private ProLearningRouteService routeService;

    @DeleteMapping("/{id}")
    public R removeRoute(@PathVariable String id){
        routeService.removeById(id);
        return R.ok();
    }

    @PutMapping("/route")
    public R addRoute(@RequestBody ProLearningRoute route){
        routeService.save(route);
        return R.ok();
    }

    @PostMapping("/route")
    public R updateMajor(@RequestBody ProLearningRoute route){
        routeService.updateById(route);
        return R.ok();
    }

    @GetMapping("/route/{id}")
    public R getRouteById(@PathVariable String id){
        ProLearningRoute route = routeService.getById(id);
        return R.ok().data("route",route);
    }

    @PostMapping("/list/{pageNo}/{limit}")
    public R listRoute(@PathVariable long pageNo,
                       @PathVariable long limit,
                       @RequestBody(required = false) ProLearningRoute learningRoute){
        QueryWrapper<ProLearningRoute> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(learningRoute.getId())){
            queryWrapper.eq("id",learningRoute.getId());
        }
        Page<ProLearningRoute> page = new Page<>(pageNo,limit);
        IPage<ProLearningRoute> routePage = routeService.page(page, queryWrapper);
        return R.ok().data("record",routePage.getRecords()).data("total",routePage.getTotal());
    }

}

